<template>
  <div class="mobile-nav-control" v-show="isMobile">
    <!-- 移动端侧边栏开关按钮 -->
    <div class="mobile-menu-toggle" @click="toggleSidebar">
      <div class="hamburger-icon" :class="{ active: sidebarOpened }">
        <span></span>
        <span></span>
        <span></span>
      </div>
    </div>
  </div>
</template>

<script>
import { isMobileViewport, addViewportChangeListener } from '@/utils/device';
import { mapGetters } from 'vuex';

export default {
  name: 'MobileNavControl',
  data() {
    return {
      isMobile: false,
      sidebarOpened: false
    }
  },
  computed: {
    ...mapGetters([
      'sidebar'
    ])
  },
  mounted() {
    // 使用设备检测工具监听视口变化
    addViewportChangeListener(this.handleViewportChange);
    
    // 同步vuex中的侧边栏状态
    this.syncSidebarState();
  },
  watch: {
    // 监听vuex中sidebar的变化，同步本地状态
    'sidebar.opened': {
      handler(val) {
        this.sidebarOpened = val;
        this.updateSidebarState();
      },
      immediate: true
    }
  },
  methods: {
    syncSidebarState() {
      // 同步vuex中的侧边栏状态
      this.sidebarOpened = this.sidebar.opened;
      this.updateSidebarState();
    },
    handleViewportChange(isMobileView) {
      this.isMobile = isMobileView;
      // 桌面端不自动关闭侧边栏，保持用户选择的状态
    },
    toggleSidebar() {
      this.sidebarOpened = !this.sidebarOpened;
      this.$store.dispatch('app/toggleSideBar');
      this.updateSidebarState();
    },
    closeSidebar() {
      this.sidebarOpened = false;
      this.$store.dispatch('app/closeSideBar', { withoutAnimation: false });
      this.updateSidebarState();
    },
    updateSidebarState() {
      // 更新侧边栏的样式类
      const sidebarEl = document.querySelector('.sidebar-container');
      
      // 只操作侧边栏，不再操作主内容区域和遮罩层
      if (sidebarEl) {
        if (this.sidebarOpened) {
          sidebarEl.classList.add('sidebar-open');
        } else {
          sidebarEl.classList.remove('sidebar-open');
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.mobile-nav-control {
  .mobile-menu-toggle {
    position: fixed;
    top: 15px;
    right: 15px;
    width: 40px;
    height: 40px;
    background-color: #1890ff;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2001; // 提高z-index确保在侧边栏之上
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    transition: all 0.3s;
    
    &:active {
      transform: scale(0.95);
    }
    
    .hamburger-icon {
      width: 20px;
      height: 16px;
      position: relative;
      
      span {
        display: block;
        position: absolute;
        height: 2px;
        width: 100%;
        background: white;
        border-radius: 2px;
        opacity: 1;
        left: 0;
        transform: rotate(0deg);
        transition: .25s ease-in-out;
        
        &:nth-child(1) {
          top: 0px;
        }
        
        &:nth-child(2) {
          top: 7px;
        }
        
        &:nth-child(3) {
          top: 14px;
        }
      }
      
      &.active {
        span {
          &:nth-child(1) {
            top: 7px;
            transform: rotate(135deg);
          }
          
          &:nth-child(2) {
            opacity: 0;
            left: -60px;
          }
          
          &:nth-child(3) {
            top: 7px;
            transform: rotate(-135deg);
          }
        }
      }
    }
  }
  
  @media screen and (min-width: 769px) {
    display: none !important;
  }
}
</style> 